package com.colin.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.app.bean.DemoBean;
import com.colin.library.base.BaseFragment;

import java.util.List;

/**
 * Created by Colin on 2017/2/10.
 */

public class ExampleFragmentRecyclerViewAdapter extends RecyclerView.Adapter<ExampleFragmentRecyclerViewAdapter.MyViewHolder> {
    private Context context;
    private List<DemoBean> demoBeanList;
    //最常见写的一种方法 ： 接口回调
    private int fragmentID = -1;//Fragment ID
    private BaseFragment.OnFragmentListener mListener = null;//接口回调

    public ExampleFragmentRecyclerViewAdapter(Context context, List<DemoBean> demoBeanList) {
        this.context = context;
        this.demoBeanList = demoBeanList;
    }

    @Override
    public ExampleFragmentRecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_example, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        //设置高度
//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.text_example_title.getLayoutParams();
//        layoutParams.height = holder.image_example_icon.getMeasuredHeight()*2;

//        holder.text_example_title.setLayoutParams(layoutParams);

        holder.text_example_title.setText(demoBeanList.get(position).getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null && fragmentID != -1) {
                    mListener.onFragmentClick(fragmentID, true, position, demoBeanList.get(position));
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return demoBeanList != null ? demoBeanList.size() : 0;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView text_example_title;
        ImageView image_example_icon;

        public MyViewHolder(View view) {
            super(view);
            text_example_title = (TextView) view.findViewById(R.id.text_example_title);
            image_example_icon = (ImageView) view.findViewById(R.id.image_example_icon);
        }
    }

    public void setOnItemClickListener(BaseFragment.OnFragmentListener mListener, int fragment_id) {
        this.mListener = mListener;
        this.fragmentID = fragment_id;
    }
}
